<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #box1{
            width: 100px;
            height: 100px;
            background-color: red;
            position: absolute;
        }
        
    </style>
    <script>
        window.onload = function(){
            var speed=10;
                var dir = 0;

            // 开启一个定时器，来控制div的移动
            setInterval(function(){
                
                switch(dir){
                    case 37:
                        box1.style.left=box1.offsetLeft - speed +"px";
                        break;
                    case 39:
                        box1.style.left=box1.offsetLeft + speed +"px";
                        break;
                    case 38:
                        box1.style.top=box1.offsetTop - speed +"px";
                        break;
                    case 40:
                        box1.style.top=box1.offsetTop + speed +"px";
                        break;
                }
                
            }, 30);

            document.onkeydown = function(event){
                event = event || window.event;
                
                if(event.ctrlKey){
                    speed = 50;
                }else{
                    speed = 10;
                }

                dir = event.keyCode;
               
            };

            document.onkeyup = function(event){
                dir=0;
            };
        };
    </script>
</head>
<body>
    <div id="box1"></div>
</body>
</html>